/*
 * @LastEditTime: 2023-02-28 14:36:34
 */
import { router } from "../index";
import { Music } from "../../Models/Music.schema";
import { MusicController } from "../../Controller/Music.controller";
import { createMusicDTO, updateMusicDTO } from "../../DTO/Music.dto";


const MusicModel = new MusicController(Music);

// 获取新歌速递
router.get("/music/newMusic", async (ctx, next) => {
  if (ctx.request.query.type)
    console.log("进来了")
  ctx.body = await MusicModel.findNewMusic(Number(ctx.request.query.type));
});

//获取歌曲
router.get("/music", async (ctx, next) => {
  // 标题查询
  if (ctx.request.query.title) {
    console.log(ctx.request.query.title)
    const res = await MusicModel.findByTitle(ctx.request.query.title.toString()) as { code: number, message: string };
    res.code === 400 ? ctx.status = 400 : null;
    ctx.body = res;
  }
  // ID 查询
  else if (ctx.request.query.id) {
    ctx.body = await MusicModel.findById(ctx.request.query.id.toString());
  }
  // 随机查询
  else if (ctx.request.query.random) {
    ctx.body = await MusicModel.findBySize(Number(ctx.request.query.random));
  }
});
//获取全部歌曲
router.get("/music/allMusic", async (ctx, next) => {
  ctx.body = await MusicModel.findAll();
});
// 创建歌曲
router.post('/music', async (ctx, next) => {
  ctx.body = await MusicModel.createItem(ctx.request.body as createMusicDTO);
});
//删除歌曲
router.delete("/music/:id", async (ctx, next) => {
  ctx.body = await MusicModel.removeById(ctx.params.id);
});
// 更新歌曲
router.patch("/music/:id", async (ctx, next) => {
  if (ctx.params.id)
    ctx.body = await MusicModel.updateById(
      ctx.params.id,
      ctx.request.body as updateMusicDTO
    );
});

//获取歌曲信息
router.get("/music/musicLink", async (ctx, next) => {
  if (ctx.request.query.id) {
    ctx.body = await MusicModel.findLinkById(ctx.request.query.id.toString());
  }
});
//获取歌曲可用性信息
router.get("/music/check", async (ctx, next) => {
  if (ctx.request.query.id) {
    ctx.body = await MusicModel.checkMusic(ctx.request.query.id.toString());
  }
});
